package org.itli.jnsocket.Service.ServiceImpl;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.itli.jnsocket.DTO.ProductDTO;
import org.itli.jnsocket.Mapper.OnBoundMapper;
import org.itli.jnsocket.Pojo.Goods;
import org.itli.jnsocket.Pojo.OnBound;
import org.itli.jnsocket.Pojo.Supplier;
import org.itli.jnsocket.Service.OnBoundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;

@Slf4j
@Service
public class OnBoundServiceImpl implements OnBoundService {

    OnBoundMapper mapper;

    @Autowired
    public void setMapper(OnBoundMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String OutBounds(String username, OnBound onBound) {

        boolean bool = true;


        try{
            //插入订单表
            Supplier supplier = null;
            for(Supplier s:onBound.getSupplier()){
                supplier = s;
            }
            int i = mapper.Insert_Purchase_Order(onBound, username, username, supplier, BigInteger.valueOf(supplier.getId()));
            if(i!=1){
                bool = false;
                throw new RuntimeException("error");
            }

            //---------------------------------------------以下是写入出库商品表以及更新库存的代码----------------------------------------------------

            //查询进货记录 id
            BigInteger pur_id = mapper.Select_id(onBound.getOrder_sn());
            if(pur_id==null){
                bool = false;
                throw new RuntimeException("error");
            }

            //由于商品过多，需要循环写入
            for(ProductDTO product : onBound.getGoods()){

                //查询商品 sn
                String sn = mapper.Select_Good_SN(product.getGoods_id());
                if(sn==null){
                    bool = false;
                    throw new RuntimeException("error");
                }

                //查询商品质量
                String spec = mapper.Select_Good_SPEC(product.getGoods_id());
                if(spec==null){
                    bool = false;
                    throw new RuntimeException("error");
                }

                product.setGoods_spec(spec);
                product.setGoods_sn(sn);

                //写入商品表
                mapper.Insert_purchase_order_goods(pur_id,onBound.getOrder_sn(),product,username,username);

                //取出库存数据
                Goods goods = null;
                int stock;


                //--------------------------------------------更新库存--------------------------------------



                try{
                    goods = mapper.Select_GoodsStock(product.getGoods_id(), onBound.getWarehouse_id());
                    if(goods==null){
                        //如果查不到，则需要添加
                        try{
                            int i1 = mapper.Insert_Stock(product.getGoods_id(), onBound.getWarehouse_id(), product.getGoods_count(), product.getGoods_sn(), onBound.getRemark(), username, username);
                        } catch (Exception e) {
                            log.error("error", e);
                            bool = false;
                            throw new RuntimeException(e);
                        }
                    }else {
                        //如果在仓库内能查出该商品，则直接修改
                        stock = goods.getGoods_stock();
                        stock+=product.getGoods_count();
                        //存入库存
                        int i1 = mapper.UpdateGoods_Stock(product.getGoods_id(), onBound.getWarehouse_id(), stock);
                        if(i1!=1){
                            bool = false;
                            throw new RuntimeException("error");
                        }
                    }
                } catch (Exception e) {
                    bool = false;
                    throw new RuntimeException(e);
                }
                //---------------------------------------------------------------------------------------

            }

        } catch (Exception e) {
            bool = false;
            throw new RuntimeException(e);
        }

        if(bool){
            return "1";
        }
        else {
            return "error";
        }
    }
}
